package cn.fl.capital.service.impl;

import cn.fl.capital.constant.CapAdvBatchConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.capital.dto.CapAdvBatchDTO;
import cn.fl.capital.condition.CapAdvBatchQueryCondition;
import cn.fl.capital.service.ICapAdvBatchApiService;
import cn.fl.capital.service.ICapAdvBatchService;
import cn.fl.capital.transform.CapAdvBatchVOTransform;
import cn.fl.capital.vo.CapAdvBatchVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.constant.SysDictionaryConstant;

import java.util.List;
import java.util.Map;

/**
 * @Description: 垫款批次表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class CapAdvBatchServiceImpl implements ICapAdvBatchService {

	/**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(CapAdvBatchServiceImpl.class);
	
	/**
	 * 垫款批次表 暴露服务
	 */
	@Autowired
	private ICapAdvBatchApiService capAdvBatchApiService;
	/**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public CapAdvBatchVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)) return null;
		CapAdvBatchDTO dto = this.capAdvBatchApiService.selectCapAdvBatchById(id);
		return CapAdvBatchVOTransform.toVO(dto);
	}

	@Override
	public Long add(CapAdvBatchVO entity) throws BaseException{
		if(null == entity) return null;
		return this.capAdvBatchApiService.addCapAdvBatch(CapAdvBatchVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)) return false;
		return this.capAdvBatchApiService.removeCapAdvBatchByIds(ids,CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(CapAdvBatchVO entity) throws BaseException{
		if(null == entity) return false;
		return this.capAdvBatchApiService.modifyCapAdvBatch(CapAdvBatchVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
        CapAdvBatchQueryCondition condition=null;
        if(null == page) {
            page = new Pager();
            page.setCondition(new CapAdvBatchQueryCondition());
        }
		condition=(CapAdvBatchQueryCondition)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.capAdvBatchApiService.searchCapAdvBatchListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<CapAdvBatchVO> voList=CapAdvBatchVOTransform.toVOList((List<CapAdvBatchDTO>)page.getResultList());
            this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
	}
	
	@Override
	public List<CapAdvBatchVO> searchList(CapAdvBatchQueryCondition condition) throws BaseException{
		if(null == condition) condition=new CapAdvBatchQueryCondition();
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
		condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<CapAdvBatchVO> voList=CapAdvBatchVOTransform.toVOList(this.capAdvBatchApiService.searchCapAdvBatchList(condition));
		this.setDictData(voList);
		return voList;
	}
	
    /**
     * 添加数据字典名称
     * @param voList
     */
	public void setDictData(List<CapAdvBatchVO> voList){
		if(CollectionUtils.isEmpty(voList)) return;
		Map<String, String> advTypCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CapAdvBatchConstant.CODE_ADV_TYP_CD);
        Map<String, String> advWayCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CapAdvBatchConstant.CODE_ADV_WAY_CD);
		for(CapAdvBatchVO vo:voList){
			if (advTypCdMap != null && advTypCdMap.containsKey(vo.getAdvTypCd())) {
            	vo.setAdvTypCdNm(advTypCdMap.get(vo.getAdvTypCd()));
        	}
        	if(advWayCdMap != null && advWayCdMap.containsKey(vo.getAdvWayCd())){
			    vo.setAdvWayCdNm(advWayCdMap.get(vo.getAdvWayCd()));
            }
		}
	}
	
	/**
     * 查询数据字典
     * @param groupCd 组代码
     * @return List<KeyValueVO>
     */
    public List<KeyValueVO> searchCdList(String groupCd){
        return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }
	    
}
